using InventoryIssue.API.Read.Application.Command.Allocate;
using InventoryIssue.Dtos;
using InventoryIssue.ErrorCode;
using MediatR;

namespace InventoryIssue.API.Read.Application.Handler.Allocate
{
    public class GetAllocateStatusHandler : IRequestHandler<GetAllocateStatusCommand, APIResult<AllocateStatusDto>>
    {
        public async Task<APIResult<AllocateStatusDto>> Handle(GetAllocateStatusCommand request, CancellationToken cancellationToken)
        {
            APIResult<AllocateStatusDto> result = new APIResult<AllocateStatusDto>();

            // 根据状态代码返回对应的状态信息
            var statusInfo = GetStatusInfo(request.StatusCode);
            
            if (statusInfo != null)
            {
                result.Data = statusInfo;
                result.Message = "查询成功";
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "未找到对应的状态信息";
                result.Code = APIEnum.Fail;
            }

            return result;
        }

        private AllocateStatusDto GetStatusInfo(int statusCode)
        {
            return statusCode switch
            {
                0 => new AllocateStatusDto
                {
                    StatusCode = 0,
                    StatusName = "待审核",
                    StatusDescription = "调拨单已创建，等待审核人员审核",
                    IsOperable = true,
                    NextOperations = new[] { "审核", "修改", "删除" }
                },
                1 => new AllocateStatusDto
                {
                    StatusCode = 1,
                    StatusName = "已审核",
                    StatusDescription = "调拨单已通过审核，等待出库操作",
                    IsOperable = true,
                    NextOperations = new[] { "出库", "修改" }
                },
                2 => new AllocateStatusDto
                {
                    StatusCode = 2,
                    StatusName = "待出库",
                    StatusDescription = "调拨单已审核通过，正在准备出库",
                    IsOperable = true,
                    NextOperations = new[] { "出库", "修改" }
                },
                3 => new AllocateStatusDto
                {
                    StatusCode = 3,
                    StatusName = "已出库",
                    StatusDescription = "商品已从源仓库出库，等待入库操作",
                    IsOperable = true,
                    NextOperations = new[] { "入库", "查看出库记录" }
                },
                4 => new AllocateStatusDto
                {
                    StatusCode = 4,
                    StatusName = "待入库",
                    StatusDescription = "商品已出库，正在运输中，等待目标仓库入库",
                    IsOperable = true,
                    NextOperations = new[] { "入库", "查看出库记录" }
                },
                5 => new AllocateStatusDto
                {
                    StatusCode = 5,
                    StatusName = "已入库",
                    StatusDescription = "商品已成功入库到目标仓库，调拨流程完成",
                    IsOperable = false,
                    NextOperations = new[] { "查看入库记录", "查看操作记录" }
                },
                6 => new AllocateStatusDto
                {
                    StatusCode = 6,
                    StatusName = "已取消",
                    StatusDescription = "调拨单已被取消，不再执行",
                    IsOperable = false,
                    NextOperations = new[] { "查看操作记录" }
                },
                _ => null
            };
        }
    }
} 